package com.lightsaber.trade.as.service.planning.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lightsaber.trade.as.dao.planning.ResourceGroupDao;
import com.lightsaber.trade.as.dao.planning.ResourceGroupItemDao;
import com.lightsaber.trade.as.entity.planning.AddResourceGroup;
import com.lightsaber.trade.as.entity.planning.AddResourceGroupItem;
import com.lightsaber.trade.as.entity.planning.Level3Planning;
import com.lightsaber.trade.as.entity.planning.ProgrammePlanningItem;
import com.lightsaber.trade.as.entity.planning.ResourceGroup;
import com.lightsaber.trade.as.entity.planning.ResourceGroupItem;
import com.lightsaber.trade.as.entity.planning.ResourceGroupPage;
import com.lightsaber.trade.as.service.planning.Level3PlanningService;
import com.lightsaber.trade.as.service.planning.ProgrammePlanningItemService;
import com.lightsaber.trade.as.service.planning.ResourceGroupService;
import com.lightsaber.trade.core.dao.BaseDao;
import com.lightsaber.trade.core.entity.AuditedObject;
import com.lightsaber.trade.core.entity.BaseEntity;
import com.lightsaber.trade.core.service.BaseServiceImpl;

@Service
@Component
@Transactional
public class ResourceGroupServiceImpl extends BaseServiceImpl<ResourceGroup, String> implements
        ResourceGroupService {

    private static final Logger log = LoggerFactory.getLogger(ResourceGroupServiceImpl.class);

    @Autowired
    private ResourceGroupDao resourceGroupDao;
    @Autowired
    private ResourceGroupItemDao resourceGroupItemDao;
    @Autowired
    private Level3PlanningService level3PlanningService;
    @Autowired
    private ProgrammePlanningItemService ppiService;

    @Override
    protected BaseDao<ResourceGroup, String> getDao() {
        return resourceGroupDao;
    }

    @Override
    public List<ResourceGroup> findByLeve3IdAndProgrammePlanningId(final String level3PlanningId,
            final String programmePlanningItemId) {
        return resourceGroupDao.findByLeve3IdAndProgrammePlanningId(level3PlanningId,
                programmePlanningItemId);
    }

    @Override
    public void deleteByLeve3IdAndProgrammePlanningId(final String level3PlanningId,
            final String programmePlanningItemId) {
        List<ResourceGroup> list = findByLeve3IdAndProgrammePlanningId(level3PlanningId,
                programmePlanningItemId);
        for (ResourceGroup rg : list) {
            List<ResourceGroupItem> rgiList = rg.getResourceGroupItems();
            for (ResourceGroupItem rgi : rgiList) {
                resourceGroupItemDao.delete(rgi);
            }
            resourceGroupDao.delete(rg);
        }
        // resourceGroupDao.deleteByLeve3IdAndProgrammePlanningId(level3PlanningId,
        // programmePlanningItemId);
    }

    @Override
    public void save(final ResourceGroup resourceGroup) {
        if (StringUtils.isBlank(resourceGroup.getId())) {
            resourceGroupDao.save(resourceGroup);
        } else {
            ResourceGroup old = get(resourceGroup.getId());
            BeanUtils.copyProperties(resourceGroup, old, new String[] { BaseEntity.ID,
                    BaseEntity.VERSION, "category", AuditedObject.CREATED_BY,
                    AuditedObject.CREATED_DATE, "level3Planning", "ProgrammePlanningItem",
                    "resourceGroupItems" });
            resourceGroupDao.update(old);
        }
    }

    @Override
    public void saveResourceGroupList(final ResourceGroupPage resourceGroupPage) {
        if (resourceGroupPage != null) {
            Level3Planning level3 = level3PlanningService.findById(resourceGroupPage
                    .getLevel3PlanningId());
            ProgrammePlanningItem ppi = ppiService.get(resourceGroupPage
                    .getProgrammePlanningItemId());
            deleteByLeve3IdAndProgrammePlanningId(level3.getId(), ppi.getId());

            for (int i = 0; i < resourceGroupPage.getResourceGroups().size(); i++) {
                AddResourceGroup arg = resourceGroupPage.getResourceGroups().get(i);
                if (arg == null) {
                    continue;
                }
                ResourceGroup rg = new ResourceGroup();
                rg.setLevel3Planning(level3);
                rg.setName(arg.getName());
                rg.setPlace(Integer.valueOf(arg.getPlace()));
                rg.setProgrammePlanningItem(ppi);
                rg.setResourceGroupItems(null);
                resourceGroupDao.save(rg);
                List<AddResourceGroupItem> rgiList = arg.getResourceGroupItems();
                if (rgiList != null) {
                    for (int j = 0; j < rgiList.size(); j++) {
                        AddResourceGroupItem argi = rgiList.get(j);
                        if (argi == null) {
                            continue;
                        }
                        ResourceGroupItem rgi = new ResourceGroupItem();
                        rgi.setCurriculumHours(Integer.valueOf(argi.getCurriculumHours()));
                        rgi.setDistribution(Float.valueOf(argi.getDistribution()));
                        rgi.setResourceGroup(rg);
                        rgi.setServingCampus(argi.getServingCampus());
                        rgi.setServingDept(argi.getServingDept());
                        resourceGroupItemDao.save(rgi);
                    }
                }
            }
        }
    }
    //
    // @Override
    // public void deleteResourceGroupList(final String rowId,
    // final ResourceGroupPage resourceGroupPage) {
    // String[] rowIds = rowId.split("_");
    // String rg_row_id = "", rgi_row_id = "";
    // rg_row_id = rowIds[1];
    // if (rowId.indexOf("rgi") != -1) {
    // rgi_row_id = rowIds[3];
    // }
    // for (int i = 0; i < resourceGroupPage.getResourceGroups().size(); i++) {
    // AddResourceGroup arg = resourceGroupPage.getResourceGroups().get(i);
    // if (arg == null)
    // continue;
    // if(StringUtils.isBlank(rgi_row_id) && Integer.valueOf(rg_row_id)==i){
    // resourceGroupDao.delete(arg.getId());
    // }
    // List<AddResourceGroupItem> rgiList = arg.getResourceGroupItems();
    // if (rgiList != null) {
    // for (int j = 0; j < rgiList.size(); j++) {
    // AddResourceGroupItem argi = rgiList.get(j);
    // if (argi == null)
    // continue;
    // if(StringUtils.isBlank(rgi_row_id) && Integer.valueOf(rg_row_id)==i){
    // resourceGroupDao.delete(arg.getId());
    // }
    // }
    // }
    // }
    // }

}
